package com.guli.order.config;

import com.guli.common.constant.MqConstant;
import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zzb
 * @Date 2024/4/21 23:01
 * @Version 1.0
 */
@Configuration
public class MqConfig {

    /*
     * 声明死信队列
     * */
    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", MqConstant.ORDER_EVENT_EXCHANGE); // 死信交换机名称
        args.put("x-dead-letter-routing-key", MqConstant.ORDER_CLOSE_ROUTING); // 死信路由键
        args.put("x-message-ttl", 300000); // 设置消息过期时间为 5 分钟
        return new Queue(MqConstant.ORDER_DELAY_QUEUE, true,
                false, false, args);
    }

    /*
     * 声明关闭订单队列
     * */
    @Bean
    public Queue orderCloseQueue() {
        return new Queue(MqConstant.ORDER_CLOSE_QUEUE, true,
                false, false);
    }

    /*
     * 声明订单交换机
     * */
    @Bean
    public Exchange orderEventExchange() {
        return new DirectExchange(MqConstant.ORDER_EVENT_EXCHANGE,
                true, false);
    }

    /*
     * 死信队列与交换机进行绑定
     * */
    @Bean
    public Binding orderDelayBinding() {
        return new Binding
                (MqConstant.ORDER_DELAY_QUEUE, Binding.DestinationType.QUEUE, MqConstant.ORDER_EVENT_EXCHANGE,
                        MqConstant.ORDER_CREATE_ROUTING, null);
    }

    /*
     * 订单队列与交换机进行绑定
     * */
    @Bean
    public Binding orderCloseBinding() {
        return new Binding
                (MqConstant.ORDER_CLOSE_QUEUE, Binding.DestinationType.QUEUE, MqConstant.ORDER_EVENT_EXCHANGE,
                        MqConstant.ORDER_CLOSE_ROUTING, null);
    }

    @Bean
    public Binding orderStockReleaseBinding() {
        return new Binding(MqConstant.STOCK_RELEASE_QUEUE, Binding.DestinationType.QUEUE, MqConstant.ORDER_EVENT_EXCHANGE,
                MqConstant.STOCK_RELEASE_ROUTING, null);
    }

    @Bean
    public Queue orderSeckillQueue() {
        return new Queue(MqConstant.ORDER_SECKILL_QUEUE, true, false,
                false, null);
    }
    /*
    *
    * */
    @Bean
    public Binding orderSeckillBinding() {
        return new Binding(MqConstant.ORDER_SECKILL_QUEUE, Binding.DestinationType.QUEUE, MqConstant.ORDER_EVENT_EXCHANGE,
                MqConstant.ORDER_SECKILL_ROUTING, null);
    }

}
